#include "CNewsCellWidget.h"

CNewsCellWidget::CNewsCellWidget(QWidget *parent): QWidget(parent), ui(new Ui::CNewsCellWidgetClass())
{
	ui->setupUi(this);
	resize(530, 160);
}

CNewsCellWidget::~CNewsCellWidget()
{
	delete ui;
}

void CNewsCellWidget::SetUi(CNewsCellData* pNewsCellData)
{
	this->m_pNewsCellData = pNewsCellData;
}

void CNewsCellWidget::mouseReleaseEvent(QMouseEvent* pQMouseEvent)
{
	Q_UNUSED(pQMouseEvent)
		emit NewsCellClick(m_pNewsCellData);
}

void CNewsCellWidget::paintEvent(QPaintEvent* pQPaintEvent)
{
	int nw = width(), nh = height(); // 500, 160
	QPainter painter(this);

	QRect rTitle(0, 0, nw * 0.6, nh * 0.5);
	painter.drawText(rTitle, m_pNewsCellData->title);

	QRect rUserName(0, nh * 0.85, nw * 0.6, nh * 0.3);
	painter.drawText(rUserName, m_pNewsCellData->userName);

	QRect rPicture(nw * 0.6, 0, nw * 0.4, nh);
	if (m_pNewsCellData->picture != CConstant::NETERRORPIC)
	{
		QPixmap pixmap;
		pixmap.loadFromData(m_pNewsCellData->pictureBytes);
		painter.drawPixmap(rPicture, pixmap);
	}
	else
	{
		QPixmap pixmap(m_pNewsCellData->picture);
		painter.drawPixmap(rPicture, pixmap);
	}

}
